package utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	/**
	 *	类似FileReference的结构, 与其不同的是 一个urlFile代表一个从url所对应的服务器文件而非本地文件 
	 * @author Roland
	 * 
	 */
	public class URLFile extends EventDispatcher
	{
		private var _urlRequest:URLRequest;
		private var _urlLoader:URLLoader;
		/**
		 *	实例化一个UrlFile 
		 * @param url
		 * @param dataFormat
		 * 
		 */
		public function URLFile(url:String,dataFormat:String = URLLoaderDataFormat.BINARY)
		{
			_urlRequest = new URLRequest(url);
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = dataFormat;
			_urlLoader.addEventListener(Event.COMPLETE,onEvent);
			_urlLoader.addEventListener(Event.OPEN,onEvent);
			_urlLoader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS,onEvent);
			_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,onEvent);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR,onEvent);
			_urlLoader.addEventListener(ProgressEvent.PROGRESS,onEvent);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onEvent);
		}
		
		
		/**
		 *	开始从服务器加载对应文件 
		 * 
		 */
		public function load():void
		{
			_urlLoader.load(_urlRequest);
		}
		
		public function get url():String
		{
			return _urlRequest.url;
		}
		
		public function get name():String
		{
			return url.replace(/.*\//g,"");
		}
		
		public function get data():*
		{
			return _urlLoader.data;
		}
		
		protected function onEvent(event:Event):void
		{
			dispatchEvent(event);
		}

		
		
	}
}